package com.woniuxy.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.Agent;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.Transaction;
import com.woniuxy.service.AgentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/agent")
public class AgentController {

    @Autowired
    private AgentService agentService;

    @RequestMapping("/byreference")
    @ResponseBody
    /**
     * @Description: 通过上级查询下级的信息，包括直属下级和简介下级
     * @Param: [id 用户id]
     * @return: com.woniuxy.entity.ResponseEntity<java.util.List<com.woniuxy.entity.Agent>>
     * @Author: zhujunxin
     * @Date: 2021/2/24
     */
    public ResponseEntity<List<Agent>> selectByReference(int id){
        List<Agent> agents = agentService.selectByReference(id);
        ResponseEntity<List<Agent>> agentEntity = new ResponseEntity<>(agents);
        return agentEntity;
    }

    @RequestMapping("/delete")
    @ResponseBody
    /**
     * @Description: 代理删除其下级代理
     * @Param: [id 需要删除下级代理的id]
     * @return: com.woniuxy.entity.ResponseEntity
     * @Author: zhujunxin
     * @Date: 2021/2/24
     */
    public ResponseEntity deleteMember(int id){
        agentService.delete(id);
        return new ResponseEntity();
    }

    @RequestMapping("/update")
    @ResponseBody
    /**
     * @Description: 上级代理更新下级代理的信息
     * @Param: [agent 需要更新的信息]
     * @return: java.lang.String
     * @Author: zhujunxin
     * @Date: 2021/2/24
     */
    public String updateById(@RequestBody Agent agent){
        int row= agentService.updateById(agent);
        if(row==1){
            return "修改成功！";
        }else {
            return "修改失败，请联系后台管理员！";
        }
    }

    @RequestMapping("/levelup")
    @ResponseBody
    /**
     * @Description: 下级代理升级后，直属上级代理和间接上级代理增加对应收益
     * @Param: [id 升级代理的id]
     * @return: com.woniuxy.entity.ResponseEntity
     * @Author: zhujunxin
     * @Date: 2021/2/24
     */
    public ResponseEntity levelUpReward(int id){
        List<Integer> ids = agentService.selectReferenceId(id);
        agentService.updateTotal(ids);
        return new ResponseEntity();
    }

    @RequestMapping("/selecttransactionall")
    @ResponseBody
    /**
     * @Description: 查询所有交易信息
     * @Param: []
     * @return: com.woniuxy.entity.ResponseEntity<java.util.List<com.woniuxy.entity.Transaction>>
     * @Author: zhujunxin
     * @Date: 2021/2/24
     */
    public ResponseEntity<List<Transaction>> selectAllTransaction(){
        List<Transaction> transactions = agentService.selectAllTransaction();
        ResponseEntity<List<Transaction>> listResponseEntity = new ResponseEntity<>(transactions);
        return listResponseEntity;
    }

    @RequestMapping("/selectbytransaction")
    @ResponseBody
    /**
     * Auther: X  <br/>
     * Date: 2021/2/24:14:43  <br/>
     * Description:通过交易信息表对当月总金额统计
     *             通过成员id进行查询所交易的金额进行统计
     */
    public ResponseEntity<PageInfo<Transaction>> selectByTransaction(Integer page ,Integer limit){
        PageInfo<Transaction> transactionList=agentService.selectTransactionPriceByMemberid(page,limit);
        ResponseEntity<PageInfo<Transaction>> pageInfoResponseEntity = new ResponseEntity<>(transactionList);
        return pageInfoResponseEntity;
    }
}
